//
//  HotGoodsView.m
//  AffectionBee
//
//  Created by 李通 on 16/8/21.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "HotGoodsView.h"
#import "FirstCellModel.h"

@interface HotGoodsView ()

@property (nonatomic,weak) UIImageView *iconImageView;

@property (nonatomic,weak) UILabel *titleLabel;

@property (nonatomic,weak) UIImageView *featuredImageView;

@property (nonatomic,weak) UIImageView *giftImageView;

@property (nonatomic,weak) UILabel *SPECLabel;

@property (nonatomic,weak) UILabel *priceLabel;

@property (nonatomic,weak) UILabel *originalLabel;

@property (nonatomic,weak) UIButton *subtractButton;

@property (nonatomic,weak) UIButton *addButton;

@property (nonatomic,weak) UILabel *conuntLabel;

@property (nonatomic,assign) NSInteger index;

@end


@implementation HotGoodsView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setContentControl];
        self.index = 0;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setContentControl {
    
    UIImageView *iconImageView = [[UIImageView alloc]init];
    self.iconImageView = iconImageView;
    [self addSubview:iconImageView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    self.titleLabel = titleLabel;
    titleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:titleLabel];
    
    UIImageView *featuredImageView = [[UIImageView alloc]init];
    self.featuredImageView = featuredImageView;
    featuredImageView.image = [UIImage imageNamed:@"jingxuan.png"];
    [self addSubview:featuredImageView];
    
    UIImageView *giftImageView = [[UIImageView alloc]init];
    self.giftImageView = giftImageView;
    giftImageView.image = [UIImage imageNamed:@"buyOne.png"];
    giftImageView.hidden = YES;
    [self addSubview:giftImageView];
    
    UILabel *SPECLabel = [[UILabel alloc]init];
    self.SPECLabel = SPECLabel;
    SPECLabel.textColor = kLightGaryColor;
    SPECLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:SPECLabel];
    
    UILabel *priceLabel = [[UILabel alloc]init];
    self.priceLabel = priceLabel;
    priceLabel.textColor = [UIColor redColor];
    priceLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:priceLabel];
    
    UILabel *originalLabel = [[UILabel alloc]init];
    self.originalLabel = originalLabel;
    originalLabel.hidden = YES;
    originalLabel.textColor = [UIColor grayColor];
    originalLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:originalLabel];
    
    UIButton *subtractButton = [[UIButton alloc]init];
    self.subtractButton = subtractButton;
    subtractButton.hidden = YES;
    [subtractButton setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    [subtractButton addTarget:self action:@selector(didClickReduce:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:subtractButton];
    
    UIButton *addButton = [[UIButton alloc]init];
    self.addButton = addButton;
    [addButton setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(didClickIncrease:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:addButton];
    
    UILabel *countLabel = [[UILabel alloc]init];
    self.conuntLabel = countLabel;
    countLabel.hidden = YES;
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.textColor = [UIColor blackColor];
    countLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:countLabel];
    
    [self setupUI];
}

- (void)setupUI {
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.contentView);
        make.height.equalTo(@(self.contentView.width));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconImageView.mas_bottom);
        make.leading.equalTo(self.contentView.mas_leading).offset(5);
        make.trailing.equalTo(self.contentView.mas_trailing);
        make.height.equalTo(@20);
    }];
    [_featuredImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom);
        make.leading.equalTo(self.contentView.mas_leading).offset(5);
        make.width.equalTo(@30);
        make.height.equalTo(@15);
    }];
    [self.giftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom);
        make.leading.equalTo(_featuredImageView.mas_trailing);
        make.width.height.equalTo(_featuredImageView);
    }];
    [self.SPECLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_titleLabel.mas_leading).offset(5);
        make.top.equalTo(_featuredImageView.mas_bottom);
        make.height.equalTo(@20);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.height.equalTo(@20);
        make.leading.equalTo(self.contentView).offset(5);
    }];
    [self.originalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(@20);
        make.leading.equalTo(_priceLabel.mas_trailing);
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView.mas_trailing).offset(-5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-5);
        make.height.equalTo(@20);
        make.width.equalTo(@20);
    }];
    [self.conuntLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(_addButton.mas_leading);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-5);
        make.height.equalTo(@20);
        make.width.equalTo(@20);
    }];
    [self.subtractButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(_conuntLabel.mas_leading);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-5);
        make.width.equalTo(@20);
        make.height.equalTo(@20);
    }];
}

- (void)didClickIncrease:(UIButton *)sender {
    self.index++;
    if (self.index > 0) {
        if (self.index > self.model.number.integerValue) {
            self.index = self.model.number.integerValue;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notice" object:nil userInfo:@{@"title":self.titleLabel.text}];
            return;
        }
        self.subtractButton.hidden = NO;
        self.conuntLabel.hidden = NO;
        self.conuntLabel.text = [NSString stringWithFormat:@"%ld",self.index];
        if (self.animationBlock) {
            self.animationBlock(self.iconImageView);
        }
    }
}

- (void)didClickReduce:(UIButton *)sender {
    self.index--;
    self.conuntLabel.text = [NSString stringWithFormat:@"%ld",self.index];
    if (self.index == 0) {
        self.subtractButton.hidden = YES;
        self.conuntLabel.hidden = YES;
    }
}

//为控件赋值
- (void)setModel:(FirstCellModel *)model {
    _model = model;
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    self.titleLabel.text = model.name;
    self.SPECLabel.text = model.specifics;
    self.priceLabel.text = [NSString stringWithFormat:@"$%@",model.partner_price];
    if (![model.market_price isEqual:@""]) {
        self.originalLabel.hidden = NO;
        NSMutableAttributedString *originalPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"$%@",model.market_price]];
        [originalPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, originalPrice.length)];
        self.originalLabel.attributedText = originalPrice;
    }else {
        self.originalLabel.hidden = YES;
    }
    if (![model.pm_desc isEqual:@""]) {
        self.giftImageView.hidden = NO;
    } else {
        self.giftImageView.hidden = YES;
    }
}


@end
